package com.chart.web.controller.chart.sta;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.chart.chart.domian.ChartConfigs;
import com.chart.chart.service.IChartConfigsService;
import com.chart.common.core.controller.BaseController;
import com.chart.common.core.domain.AjaxResult;
import com.chart.common.utils.ExcelDataUtils;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 盒须图
 *
 * @author chart
 */
@RestController
@RequestMapping("/boxWhiskers/chart")
public class BoxWhiskersChartController extends BaseController
{


    @Autowired
    private IChartConfigsService chartConfigsService;




    //盒须图
    @PostMapping("/importData1")
    public AjaxResult importData(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);
            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> groupName = new ArrayList<>();

            List<List<Object>> gruopDatas = new ArrayList<>();


            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            List<JSONObject> dataset = (List<JSONObject>) jsonObject.get("dataset");

            List<JSONObject> datasetList = new ArrayList<>();


            for (int i = 1; i < getdata.size(); i++) {
                List<Object> gruopData = new ArrayList<>();
                List<String> colData =new ArrayList<>();
                List<String> data = getdata.get(i);
                for (int i1 = 0; i1 < data.size(); i1++) {
                    String s = data.get(i1);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        colData.add(s);
                    }
                }

                if (!CollectionUtils.isEmpty(colData)) {
                    if (i == getdata.size()-1) {
                        JSONObject jsonObject1 = new JSONObject();
                        for (int i1 = 0; i1 < colData.size(); i1++) {
                            gruopData.add(Double.parseDouble(colData.get(i1)));
                        }
                        gruopDatas.add(gruopData);
                        jsonObject1.put("source", gruopDatas);
                        datasetList.add(jsonObject1);
                    } else if (i == 1) {
                        groupName.add(colData.get(0));
                    } else {
                        if (colData.size() == 1) {
                            groupName.add(colData.get(0));
                            JSONObject jsonObject1 = new JSONObject();
                            jsonObject1.put("source", gruopDatas);
                            datasetList.add(jsonObject1);
                            gruopDatas = new ArrayList<>();
                        } else  {
                            for (int i1 = 0; i1 < colData.size(); i1++) {
                                gruopData.add(Double.parseDouble(colData.get(i1)));
                            }
                            gruopDatas.add(gruopData);
                        }
                    }
                }
            }

            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            ArrayList<JSONObject> seriesList = new ArrayList<>();
            JSONObject jsonObject1 = series.get(0);

            JSONObject jsonObject2 = dataset.get(dataset.size() - 1);
            for (int i = 0; i < groupName.size(); i++) {
                JSONObject jsonObject3 = new JSONObject();
                jsonObject3.putAll(jsonObject2);
                jsonObject3.put("fromDatasetIndex", i);
                datasetList.add(jsonObject3);

                JSONObject jsonObject4 = new JSONObject();
                jsonObject4.putAll(jsonObject1);
                jsonObject4.put("name", groupName.get(i));
                jsonObject4.put("datasetIndex", i + groupName.size());
                seriesList.add(jsonObject4);
            }
            jsonObject.put("dataset", datasetList);

            jsonObject.put("series", seriesList);

            JSONObject legend = (JSONObject) jsonObject.get("legend");

            ArrayList<Map> maps = new ArrayList<>();
            for (int i = 0; i < groupName.size(); i++) {
                Map map = new HashMap<>();
                map.put("name",groupName.get(i));
                maps.add(map);
            }

            legend.put("data",maps);
            jsonObject.put("legend",legend);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }


}
